<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-form">
        <div class="form-header">
          <h1 class="form-title">用户注册</h1>
          <p class="form-subtitle">注册成功后将自动开通银行卡</p>
        </div>

        <el-form
          ref="registerFormRef"
          :model="registerForm"
          :rules="registerRules"
          class="register-form-content"
          size="large"
          label-width="100px"
        >
          <el-form-item label="用户姓名" prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入真实姓名"
              clearable
            />
          </el-form-item>

          <el-form-item label="身份证号" prop="pid">
            <el-input
              v-model="registerForm.pid"
              placeholder="请输入18位身份证号"
              clearable
            />
          </el-form-item>

          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="registerForm.gender">
              <el-radio label="男">男</el-radio>
              <el-radio label="女">女</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="联系电话" prop="telephone">
            <el-input
              v-model="registerForm.telephone"
              placeholder="请输入11位手机号"
              clearable
            />
          </el-form-item>

          <el-form-item label="登录账号" prop="account">
            <el-input
              v-model="registerForm.account"
              placeholder="请输入登录账号"
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              @click="handleRegister"
              style="width: 100%"
            >
              立即注册
            </el-button>
          </el-form-item>

          <div class="form-footer">
            <el-button type="text" @click="goToLogin">
              已有账号？立即登录
            </el-button>
            <el-button type="text" @click="goToHome">
              返回首页
            </el-button>
          </div>
        </el-form>
      </div>

      <div class="register-banner">
        <div class="banner-content">
          <h2>加入我们</h2>
          <p>成为我们的用户，享受专业的银行服务</p>
          
          <div class="benefits-list">
            <div class="benefit-item">
              <el-icon><CircleCheck /></el-icon>
              <span>免费开户，无年费</span>
            </div>
            <div class="benefit-item">
              <el-icon><CircleCheck /></el-icon>
              <span>实时转账，秒到账</span>
            </div>
            <div class="benefit-item">
              <el-icon><CircleCheck /></el-icon>
              <span>24小时客服支持</span>
            </div>
            <div class="benefit-item">
              <el-icon><CircleCheck /></el-icon>
              <span>安全保障，资金无忧</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userApi } from '@/api/user'
import { CircleCheck } from '@element-plus/icons-vue'

export default {
  name: 'Register',
  components: {
    CircleCheck
  },
  setup() {
    const router = useRouter()
    
    const registerFormRef = ref()
    const loading = ref(false)
    
    const registerForm = reactive({
      username: '',
      pid: '',
      gender: '',
      telephone: '',
      account: ''
    })
    
    // 身份证号校验
    const validatePid = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入身份证号'))
      } else if (!/^\d{17}[\dXx]$/.test(value)) {
        callback(new Error('请输入正确的18位身份证号'))
      } else {
        callback()
      }
    }
    
    // 手机号校验
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入手机号'))
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的11位手机号'))
      } else {
        callback()
      }
    }
    
    const registerRules = {
      username: [
        { required: true, message: '请输入用户姓名', trigger: 'blur' },
        { min: 2, max: 10, message: '姓名长度在2到10个字符', trigger: 'blur' }
      ],
      pid: [
        { validator: validatePid, trigger: 'blur' }
      ],
      gender: [
        { required: true, message: '请选择性别', trigger: 'change' }
      ],
      telephone: [
        { validator: validatePhone, trigger: 'blur' }
      ],
      account: [
        { required: true, message: '请输入登录账号', trigger: 'blur' },
        { min: 4, max: 20, message: '账号长度在4到20个字符', trigger: 'blur' }
      ]
    }

    const handleRegister = async () => {
      if (!registerFormRef.value) return
      
      try {
        await registerFormRef.value.validate()
        loading.value = true
        
        await userApi.register(registerForm)
        
        ElMessage.success('注册成功！请使用您的账号登录')
        
        // 跳转到登录页面
        router.push('/login')
        
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error(error.message || '注册失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }

    const goToLogin = () => {
      router.push('/login')
    }

    const goToHome = () => {
      router.push('/')
    }

    return {
      registerFormRef,
      loading,
      registerForm,
      registerRules,
      handleRegister,
      goToLogin,
      goToHome
    }
  }
}
</script>

<style lang="scss" scoped>
.register-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-container {
  display: flex;
  width: 100%;
  max-width: 1200px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.register-form {
  flex: 1;
  padding: 40px;
  
  .form-header {
    text-align: center;
    margin-bottom: 40px;
    
    .form-title {
      font-size: 32px;
      font-weight: 700;
      color: #333;
      margin-bottom: 8px;
    }
    
    .form-subtitle {
      color: #666;
      font-size: 16px;
    }
  }
  
  .register-form-content {
    max-width: 500px;
    margin: 0 auto;
    
    .el-form-item {
      margin-bottom: 24px;
    }
    
    .el-input {
      height: 44px;
      
      .el-input__wrapper {
        border-radius: 8px;
      }
    }
    
    .el-radio-group {
      .el-radio {
        margin-right: 30px;
      }
    }
  }
  
  .form-footer {
    display: flex;
    justify-content: space-between;
    margin-top: 24px;
    
    .el-button--text {
      color: #1890ff;
      
      &:hover {
        color: #40a9ff;
      }
    }
  }
}

.register-banner {
  flex: 1;
  background: linear-gradient(45deg, #52c41a, #13c2c2);
  color: white;
  padding: 60px 40px;
  display: flex;
  align-items: center;
  
  .banner-content {
    h2 {
      font-size: 32px;
      font-weight: 600;
      margin-bottom: 16px;
    }
    
    p {
      font-size: 18px;
      opacity: 0.9;
      line-height: 1.6;
      margin-bottom: 40px;
    }
    
    .benefits-list {
      .benefit-item {
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 20px;
        
        .el-icon {
          font-size: 20px;
          color: #f6ffed;
        }
        
        span {
          font-size: 16px;
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    max-width: 500px;
  }
  
  .register-form {
    padding: 30px 20px;
    
    .register-form-content {
      .el-form-item {
        .el-form-item__label {
          width: 80px !important;
        }
      }
    }
  }
  
  .register-banner {
    padding: 40px 30px;
    text-align: center;
    
    .banner-content {
      h2 {
        font-size: 24px;
      }
      
      .benefits-list {
        .benefit-item {
          justify-content: center;
        }
      }
    }
  }
}
</style> 